package com.egoo.ticket.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.egoo.ticket.server.pojo.dto.MsgNotifySettingDto;
import com.egoo.ticket.server.pojo.entity.MsgNotifySetting;
import com.egoo.ticket.server.pojo.entity.MsgSettingRelation;
import com.egoo.ticket.server.pojo.vo.MsgNotifySettingVo;
import com.egoo.ticket.server.service.IMsgNotifySettingService;
import com.egoo.ticket.server.service.IMsgSettingRelationService;
import com.egoo.ticket.server.utils.common.ConstantCode;
import com.egoo.ticket.server.utils.common.DbResponse;
import com.egoo.ticket.server.utils.common.ResponseConstant;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author donny
 * @since 2020-10-29
 */
@Log4j2
@RestController
@RequestMapping("/v1/msgNotifySetting")
@Api(value = "消息通知设置", tags = "消息通知设置",description = "消息通知设置")
public class MsgNotifySettingController extends BaseController {

	@Autowired
	private IMsgNotifySettingService iMsgNotifySettingService;
	@Autowired
	private IMsgSettingRelationService iMsgSettingRelationService;


	@RequestMapping(method = RequestMethod.POST,value = "/insert")
	@ApiOperation(value = "新增消息通知设置",notes = "新增消息通知设置")
	public DbResponse insert(@RequestBody MsgNotifySettingDto msgNotifySettingDto
							 ){

		if (headerParamIsExist()){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"userId和tenantId不能为空");
		}
		msgNotifySettingDto.setTenantId(tenantId)
							.setUserId(userId);
		Long workOrderTypeId = msgNotifySettingDto.getWorkOrderTypeId();
		String notifyType = msgNotifySettingDto.getNotifyType();
		List<MsgSettingRelation> msgSettingRelations = msgNotifySettingDto.getMsgSettingRelations();
		if (workOrderTypeId == null){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"工单类型不能为空");
		}
		if (StringUtils.isEmpty(notifyType)){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"通知类型不能为空");
		}
		if (CollectionUtils.isEmpty(msgSettingRelations)){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"请选择发送对象和消息类型");
		}
		try {
			boolean b = iMsgNotifySettingService.insertMsgNotify(msgNotifySettingDto);

				return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,ResponseConstant.ADD_SUCCESS_MSG);

		} catch (Exception e) {
			log.info("新增异常"+e);
		}
		return new DbResponse(ResponseConstant.RES_FAIL_CODE,ResponseConstant.ADD_FAIL_MSG);
	}



	@RequestMapping(method = RequestMethod.POST,value = "/delete")
	@ApiOperation(value = "删除消息通知设置",notes = "删除消息通知设置")
	public DbResponse delete(@RequestBody MsgNotifySettingDto msgNotifySettingDto) {
		if (headerParamIsExist()){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"userId和tenantId不能为空");
		}
		List<Integer> ids = msgNotifySettingDto.getIds();
		msgNotifySettingDto.setTenantId(tenantId)
				.setUserId(userId);
		if (CollectionUtils.isEmpty(ids)){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"请选中要删除的项");
		}
		boolean b = iMsgNotifySettingService.removeByIds(ids);
		if (b){
			QueryWrapper<MsgSettingRelation> queryWrapper = new QueryWrapper<>();
			queryWrapper.lambda().in(MsgSettingRelation::getMsgNotifyId,ids);
			iMsgSettingRelationService.remove(queryWrapper);
			return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,ResponseConstant.DEL_SUCCESS_MSG);
		}
		return new DbResponse(ResponseConstant.RES_FAIL_CODE,ResponseConstant.DEL_FAIL_MSG);
	}

	@RequestMapping(method = RequestMethod.POST,value = "/update")
	@ApiOperation(value = "更新消息通知设置",notes = "更新消息通知设置")
	public DbResponse update(@RequestBody MsgNotifySettingDto msgNotifySettingDto
							 ) {
		if (headerParamIsExist()){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"userId和tenantId不能为空");
		}
		msgNotifySettingDto.setTenantId(tenantId)
				.setUserId(userId);
		Long workOrderTypeId = msgNotifySettingDto.getWorkOrderTypeId();
		String notifyType = msgNotifySettingDto.getNotifyType();
		List<MsgSettingRelation> msgSettingRelations = msgNotifySettingDto.getMsgSettingRelations();
		if (workOrderTypeId == null){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"工单类型不能为空");
		}
		if (StringUtils.isEmpty(notifyType)){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"通知类型不能为空");
		}
		if (CollectionUtils.isEmpty(msgSettingRelations)){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"请选择发送对象和消息类型");
		}
		boolean b = iMsgNotifySettingService.updateNotifySetting(msgNotifySettingDto);

		if (b){
			return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,ResponseConstant.UPDATE_SUCCESS_MSG);
		}
		return new DbResponse(ResponseConstant.RES_FAIL_CODE,ResponseConstant.UPDATE_FAIL_MSG);
	}

	@RequestMapping(method = RequestMethod.POST,value = "/forbidden")
	@ApiOperation(value = "启用/禁用消息通知设置",notes = "启用/禁用消息通知设置")
	public DbResponse forbidden(@RequestBody MsgNotifySettingDto msgNotifySettingDto) {
		if (headerParamIsExist()){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"userId和tenantId不能为空");
		}
		Integer id = msgNotifySettingDto.getId();
		Integer status = msgNotifySettingDto.getStatus();
		msgNotifySettingDto.setTenantId(tenantId)
				.setUserId(userId);
		if (id == null){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"id不能为空");
		}
		if (status == null){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"状态值不能为空");
		}
		MsgNotifySetting msgNotifySetting = new MsgNotifySetting();
		QueryWrapper<MsgNotifySetting> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(MsgNotifySetting::getId,id);
		msgNotifySetting.setStatus(status);
		boolean update = iMsgNotifySettingService.update(msgNotifySetting, queryWrapper);
		if (ConstantCode.STATUS_CLOSED_OLD.equals(status) && update ){
			return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,"禁用成功");
		}
		if (ConstantCode.STATUS_NOMAL_OLD.equals(status) && update){
			return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,"启用成功");
		}
		return new DbResponse(ResponseConstant.RES_FAIL_CODE,"操作失败");
	}

	@RequestMapping(method = RequestMethod.POST,value = "/getList")
	@ApiOperation(value = "消息通知设置列表",notes = "消息通知设置列表(查询)")
	public DbResponse getList(@RequestBody MsgNotifySettingDto msgNotifySettingDto) {

		if (headerParamIsExist()){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"userId和tenantId不能为空");
		}
		Integer pageNumber = msgNotifySettingDto.getPageNumber();
		Integer pageSize = msgNotifySettingDto.getPageSize();
		if (pageNumber == null){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"页码不能为空");
		}
		if (pageSize == null){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"每页数量不能为空");
		}
		msgNotifySettingDto.setTenantId(tenantId)
				.setUserId(userId);
		try {
			List<MsgNotifySettingVo> list = iMsgNotifySettingService.getList(msgNotifySettingDto);
			PageInfo<MsgNotifySettingVo> pageInfo = new PageInfo<>(list);
			long total = pageInfo.getTotal();
			return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,ResponseConstant.SELECT_SUCCESS_MSG,list,total);
		} catch (Exception e) {
			e.printStackTrace();
			log.info("查询异常"+e);
		}
		return new DbResponse(ResponseConstant.RES_FAIL_CODE,ResponseConstant.SELECT_FAIL_MSG);
	}

	@RequestMapping(method = RequestMethod.GET,value = "/byId")
	@ApiOperation(value = "通过id查询消息通知设置",notes = "通过id查询消息通知设置")
	public DbResponse byId(@RequestParam("id") Integer id) {
		if (headerParamIsExist()){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"userId和tenantId不能为空");
		}
		if (id == null){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"id不能为空");
		}
		try {
			MsgNotifySettingDto msgNotifySettingDto = iMsgNotifySettingService.byId(id);
			return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,ResponseConstant.SELECT_SUCCESS_MSG,msgNotifySettingDto);
		} catch (Exception e) {
			e.printStackTrace();
			log.info("查询异常"+e);
		}
		return new DbResponse(ResponseConstant.RES_FAIL_CODE,ResponseConstant.SELECT_FAIL_MSG);
	}
}
